//
//  DataRequest.m
//  ZYYD
//
//  Created by yuzhou on 12-8-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DataRequest.h"
#import "Constant.h"
#import "JSON.h"
//#import "AuthorManager.h"
#import <CommonCrypto/CommonDigest.h>
#import "GTMBase64.h"
#import "ASIDownloadCache.h"
#import "AuthorManager.h"

@implementation DataRequest

static DataRequest *dataRequest;
+ (id)sharedRequest
{
    if (dataRequest) {
        return dataRequest;
    }
    dataRequest = [[DataRequest alloc] init];
    return dataRequest;
}

- (id)init
{
    self = [super init];
    if (self) {
        queue = [[ASINetworkQueue alloc] init];
        queue.showAccurateProgress = YES;
        [queue go];
        [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
    }
    return self;
}

//首页广告
- (void)getAd
{
    NSString *urlStr = [NSString stringWithFormat:AdURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotAdNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotAdNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//首页推荐
- (void)getFeatured
{
    NSString *urlStr = [NSString stringWithFormat:FeaturedURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotFeaturedNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotFeaturedNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//评书信息
- (void)getBookInfoByID:(NSString *)itemid
{
    NSString *urlStr = [NSString stringWithFormat:BookInfoURL,[[AuthorManager defaultManager] userID],itemid];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotBookInfoNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotBookInfoNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}


//评书目录
- (void)getBookContentByID:(NSString *)itemid
{
    NSString *urlStr = [NSString stringWithFormat:BookContentURL,[[AuthorManager defaultManager] userID],itemid];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotBookContentNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotBookContentNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//评书评论
- (void)getBookCommentByID:(NSString *)itemid
{
    NSString *urlStr = [NSString stringWithFormat:BookCommentURL,[[AuthorManager defaultManager] userID],itemid];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotBookCommentNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotBookCommentNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//频道分类
- (void)getCategory
{
    NSString *urlStr = [NSString stringWithFormat:CategoryURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotCategoryNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotCategoryNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//频道内容
- (void)getChannelByID:(NSString *)class_id
{
    NSString *urlStr = [NSString stringWithFormat:ChannelURL,[[AuthorManager defaultManager] userID],class_id];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotChannelNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotChannelNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//用户登录
- (void)loginWithUsername:(NSString *)username password:(NSString *)password
{
    NSString *urlStr = [NSString stringWithFormat:LoginURL,username,password];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",loginNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.requestMethod = @"POST";
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:loginNotify,@"notify", nil];
    [queue addOperation:request];
}

//用户注册
- (void)registerWithUsername:(NSString *)username password:(NSString *)password email:(NSString *)email
{
    NSString *urlStr = [NSString stringWithFormat:RegisterURL,username,password,email];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",registerNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.requestMethod = @"POST";
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:registerNotify,@"notify", nil];
    [queue addOperation:request];
}

//修改密码
- (void)modifyPasswordWithOriginPassword:(NSString *)originPassowrd newPassword:(NSString *)newPassword
{
    NSString *urlStr = [NSString stringWithFormat:ModifyURL,[[AuthorManager defaultManager] username],originPassowrd,newPassword];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",modifyNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.requestMethod = @"POST";
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:modifyNotify,@"notify", nil];
    [queue addOperation:request];
}

//收藏评书
- (void)collectBook:(NSString *)userID username:(NSString *)username bookID:(NSString *)bookID {
    NSString *urlStr = [NSString stringWithFormat:CollectBookURL,userID,username,bookID];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",collectBookNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:collectBookNotify,@"notify", nil];
    [queue addOperation:request];
}

//获取收藏评论分享次数
- (void)getUserActions
{
    NSString *urlStr = [NSString stringWithFormat:UserActionsURL,[[AuthorManager defaultManager] userID],[[AuthorManager defaultManager] username]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotUserActionsNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotUserActionsNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//获取已收藏评书
- (void)getCollected
{
    NSString *urlStr = [NSString stringWithFormat:CollectedURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotCollectedNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotCollectedNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//获取已评论评书
- (void)getCommented
{
    NSString *urlStr = [NSString stringWithFormat:CommentedURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotCommentedNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotCommentedNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

//获取已分享评书
- (void)getShared
{
    NSString *urlStr = [NSString stringWithFormat:SharedURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotSharedNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotSharedNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

- (void)getKeywords
{
    NSString *urlStr = [NSString stringWithFormat:KeywordsURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotKeywordsNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotKeywordsNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

- (void)getBookWithKeywords:(NSString *)keywords
{
    NSString *urlStr = [NSString stringWithFormat:SearchURL,[[AuthorManager defaultManager] userID],keywords];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotSearchNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotSearchNotify,@"notify", nil];
    [queue addOperation:request];
}

- (void)getArtists
{
    NSString *urlStr = [NSString stringWithFormat:ArtistsURL,[[AuthorManager defaultManager] userID]];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotArtistsNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotArtistsNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

- (void)getArtistWorkWithAritist:(NSString *)artistID
{
    NSString *urlStr = [NSString stringWithFormat:ArtistWorkURL,[[AuthorManager defaultManager] userID],artistID];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotArtistWorkNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotArtistWorkNotify,@"notify", nil];
    request.cachePolicy = ASIFallbackToCacheIfLoadFailsCachePolicy;
    request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
    request.secondsToCache = 3*24*60*60;
    [queue addOperation:request];
}

- (void)commentBookWithID:(NSString *)itemid comment:(NSString *)comment rate:(int)rate
{
    NSString *urlStr = [NSString stringWithFormat:CommentBookURL,[[AuthorManager defaultManager] userID],[[AuthorManager defaultManager] username],itemid,comment,rate];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotCommentBookNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotCommentBookNotify,@"notify", nil];
    [queue addOperation:request];
}

- (void)getAppUpdateInfo
{
    NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] valueForKey:(NSString *)kCFBundleVersionKey];
    NSString *urlStr = [NSString stringWithFormat:AppUpdateURL,localVersion];
    NSLog(@">>>>发起请求>>>>%@>>>>:%@",gotAppUpdateInfoNotify,urlStr);
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.delegate = self;
    request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:gotAppUpdateInfoNotify,@"notify", nil];
    [queue addOperation:request];
}

//请求成功
- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *notify = [request.userInfo valueForKey:@"notify"];
//    NSLog(@">>>>接口返回数据>>>>%@>>>>:%@",notify,request.responseString);
    id result = [request.responseString JSONValue];
    
    if (result && notify) {
        if ([notify isEqualToString:gotCategoryNotify]) {
            if ([result isKindOfClass:[NSDictionary class]]) {
                NSArray *array = (NSArray *)[result valueForKey:@"result"];
                self.categoryArray = array;
            }
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:notify object:result userInfo:request.userInfo];
    }
}

//请求失败
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSString *notify = [request.userInfo valueForKey:@"notify"];
    NSLog(@">>>>>请求失败>>>>>>%@",notify);
    [[NSNotificationCenter defaultCenter] postNotificationName:notify object:nil userInfo:request.userInfo];
    
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"网络连接失败"] autorelease];
    [promptView show];
}

- (NSArray *)categoryArray
{
    if (_categoryArray == nil) {
//        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"无法获取频道"] autorelease];
//        [promptView show];
        return nil;
    }
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"artists",@"class_id",@"艺术家",@"class_name", nil];
    NSMutableArray *array = [NSMutableArray arrayWithObject:dic];
    [array addObjectsFromArray:_categoryArray];
    return array;
}

- (void)dealloc
{
    [_categoryArray release];
    [super dealloc];
}
@end
